package com.ss.android.common.applog;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.core.encrypt.TTEncryptUtils;
import com.ss.android.common.AppContext;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.f.DeviceRegisterManager;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.GZIPOutputStream;

public final class NetUtil {
    public interface IAppParam {
        void getSSIDs(Context context, Map<String, String> arg2);
    }

    public static final class b {
        public b() {

        }
    }

    private static IAppParam sAppParam;
    private static AppContext b = null;
    private static volatile boolean c;
    private static volatile boolean d;
    private static volatile int appLogEncryptFaildCount;
    private static Object mLock = new Object();
    private static volatile String APP_LOG_ENCRYPT_COUNT = "app_log_encrypt_switch_count";
    private static final ConcurrentHashMap<String, String> h = new ConcurrentHashMap();
    private static volatile b i;
    private static IAliYunHandler sAliYunHandler;

    public static void a(Map<String, String> queryMap, boolean arg10) {
        Object v1_1;
        Iterator v2_1;
        String v0_3;
        int v8 = 10;
        AppContext appContext = NetUtil.b;
        if (queryMap != null && appContext != null) {
            HashMap<String, String> idMap = new HashMap();
            try {
                if (ToolUtils.isMessageProcess(appContext.getContext())) {
                    if (NetUtil.sAppParam != null) {
                        NetUtil.sAppParam.getSSIDs(appContext.getContext(), idMap);
                    }

                    if (Logger.debug()) {
                        Logger.d("PushService", "idmap = " + android.support.design.a.a(idMap));
                    }
                } else {
                    DeviceRegisterManager.a(idMap);
                }
            } catch (Exception exception) {
                DeviceRegisterManager.a(idMap);
            }
            String v0_1 = idMap.get("install_id");
            if (!android.support.design.a.isTextEmpty(((String) v0_1))) {
                queryMap.put("iid", v0_1);
            }

            v0_1 = idMap.get("device_id");
            if (!android.support.design.a.isTextEmpty(((String) v0_1))) {
                queryMap.put("device_id", v0_1);
            }

            Context context = appContext.getContext();
            if (context != null) {
                v0_3 = android.support.design.a.doGetNetworkAccessType(android.support.design.a.getNetworkType(context));
                if (!android.support.design.a.isTextEmpty(v0_3)) {
                    queryMap.put("ac", v0_3);
                }
            }

            v0_3 = appContext.getTweakedChannel();
            if (v0_3 != null) {
                queryMap.put("channel", v0_3);
            }

            queryMap.put("aid", String.valueOf(appContext.getAid()));
            v0_3 = appContext.getAppName();
            if (v0_3 != null) {
                queryMap.put("app_name", v0_3);
            }

            queryMap.put("version_code", String.valueOf(appContext.getVersionCode()));
            queryMap.put("version_name", appContext.getVersion());
            queryMap.put("device_platform", "android");
            v0_3 = appContext.getAbVersion();
            if (!android.support.design.a.isTextEmpty(v0_3)) {
                queryMap.put("ab_version", v0_3);
            }

            v0_3 = appContext.getAbClient();
            if (!android.support.design.a.isTextEmpty(v0_3)) {
                queryMap.put("ab_client", v0_3);
            }

            v0_3 = appContext.getAbGroup();
            if (!android.support.design.a.isTextEmpty(v0_3)) {
                queryMap.put("ab_group", v0_3);
            }

            v0_3 = appContext.getAbFeature();
            if (!android.support.design.a.isTextEmpty(v0_3)) {
                queryMap.put("ab_feature", v0_3);
            }

            long v4 = appContext.getAbFlag();
            if (v4 > 0) {
                queryMap.put("abflag", String.valueOf(v4));
            }

            if (arg10) {
                queryMap.put("ssmix", "a");
            }

            queryMap.put("device_type", Build.MODEL);
            queryMap.put("device_brand", Build.BRAND);
            queryMap.put("language", Locale.getDefault().getLanguage());
            queryMap.put("os_api", String.valueOf(Build.VERSION.SDK_INT));
            try {
                v0_3 = Build.VERSION.RELEASE;
                if (v0_3 != null && v0_3.length() > v8) {
                    v0_3 = v0_3.substring(0, 10);
                }

                queryMap.put("os_version", v0_3);
            } catch (Exception v0) {
            }

            v0_3 = appContext.getDeviceId();
            if (!NetUtil.a(v0_3)) {
                queryMap.put("uuid", v0_3);
            }

            v0_1 = idMap.get("openudid");
            if (!android.support.design.a.isTextEmpty(((String) v0_1))) {
                queryMap.put("openudid", v0_1);
            }

            if (NetUtil.sAliYunHandler != null) {
                String cloudUUID = NetUtil.sAliYunHandler.getCloudUUID();
                if (!TextUtils.isEmpty(cloudUUID)) {
                    queryMap.put("aliyun_uuid", cloudUUID);
                }
            }

            queryMap.put("manifest_version_code", String.valueOf(appContext.getManifestVersion()));
            v0_3 = UIUtils.d(appContext.getContext());
            if (!android.support.design.a.isTextEmpty(v0_3)) {
                queryMap.put("resolution", v0_3);
            }

            int dpi = UIUtils.getDensityDpi(appContext.getContext());
            if (dpi > 0) {
                queryMap.put("dpi", String.valueOf(dpi));
            }

            queryMap.put("update_version_code", String.valueOf(appContext.getUpdateVersionCode()));
            queryMap.put("_rticket", String.valueOf(System.currentTimeMillis()));
            try {
                if (!NetUtil.h.isEmpty()) {
                    Iterator<Map.Entry<String, String>> iterator = NetUtil.h.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, String> entry = iterator.next();
                        if (entry == null) {
                            continue;
                        }
                        String key = entry.getKey();
                        String value = entry.getValue();
                        if (android.support.design.a.isTextEmpty(((String) key))) {
                            continue;
                        }

                        if (android.support.design.a.isTextEmpty((value))) {
                            continue;
                        }

                        queryMap.put(key, value);
                    }
                }
            } catch (Exception exception) {
            }

            try {
                if (NetUtil.i == null) {
                    return;
                }

                HashMap<String, String> hashMap = new HashMap();
                hashMap.put("rom_version", com.ss.android.article.common.c.a.a());
                if (hashMap.isEmpty()) {
                    return;
                }

                Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> entry = iterator.next();
                    if (entry == null) {
                        continue;
                    }

                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (android.support.design.a.isTextEmpty(((String) key))) {
                        continue;
                    }

                    if (android.support.design.a.isTextEmpty(((String) value))) {
                        continue;
                    }

                    if (queryMap.containsKey(key)) {
                        continue;
                    }
                    queryMap.put(key, value);
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

    public static void a(IAppParam arg0) {
        NetUtil.sAppParam = arg0;
    }

    public static void a(b arg1) {
        if (NetUtil.i == null) {
            NetUtil.i = arg1;
        }
    }

    public static String a(String arg2, boolean arg3) {
        AppContext v0 = NetUtil.b;
        if (!android.support.design.a.isTextEmpty(arg2) && v0 != null) {
            StringBuilder v0_1 = new StringBuilder(arg2);
            NetUtil.a(v0_1, arg3);
            arg2 = v0_1.toString();
        }

        return arg2;
    }

    public static void a(IAliYunHandler arg0) {
        NetUtil.sAliYunHandler = arg0;
    }

    public static void a(StringBuilder arg5, boolean arg6) {
        if (NetUtil.b != null && arg5 != null) {
            if (arg5.toString().indexOf(63) < 0) {
                arg5.append("?");
            } else {
                arg5.append("&");
            }

            LinkedHashMap v0 = new LinkedHashMap();
            NetUtil.a(((Map) v0), arg6);
            ArrayList v1 = new ArrayList();
            Iterator v2 = ((Map) v0).entrySet().iterator();
            while (v2.hasNext()) {
                Object v0_1 = v2.next();
                ((List) v1).add(new Pair(((Map.Entry) v0_1).getKey(), ((Map.Entry) v0_1).getValue()));
            }

            arg5.append(android.support.design.a.a(((List) v1), "UTF-8"));
        }
    }

    public static boolean a(String arg5) {
        boolean v1 = false;
        boolean v2 = true;
        if (!android.support.design.a.isTextEmpty(arg5) && !arg5.equalsIgnoreCase("unknown")) {
            int v0 = 0;
            while (true) {
                if (v0 >= arg5.length()) {
                    break;
                } else if (arg5.charAt(v0) == 48) {
                    ++v0;
                    continue;
                }
                return v1;
            }

            v1 = true;
            v2 = v1;
        }

        return v2;
    }

    @Deprecated
    public static int a() {
        return 1;
    }

    public static String a(String arg8, byte[] bytes, Context context, boolean arg11) throws Exception {
        byte[] v2;
        String v0_2;
        com.bytedance.common.utility.e.a v1 = null;
        int v0 = 1;
        if (!android.support.design.a.isTextEmpty(arg8) && bytes != null && bytes.length > 0) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8192);
            GZIPOutputStream gzipOutputStream = new GZIPOutputStream(((OutputStream) byteArrayOutputStream));
            try {
                gzipOutputStream.write(bytes);
            } catch (Throwable throwable) {
                Logger.w("AppLog", "compress with gzip exception: " + throwable);
                return null;
            } finally {
                try {
                    gzipOutputStream.close();
                } catch (Exception e) {

                }
            }

            gzipOutputStream.close();
            byte[] bytes2 = byteArrayOutputStream.toByteArray();
            if (!NetUtil.c) {
                synchronized (mLock) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences(NetUtil.APP_LOG_ENCRYPT_COUNT, 0);
                    NetUtil.appLogEncryptFaildCount = sharedPreferences.getInt("app_log_encrypt_faild_count", 0);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("app_log_encrypt_faild_count", NetUtil.appLogEncryptFaildCount + 1);
                    editor.apply();
                    NetUtil.c = true;
                }
            }
            if (NetUtil.appLogEncryptFaildCount < 3) {
                v2 = TTEncryptUtils.encryptBytes(bytes2, bytes2.length);
                if (!NetUtil.d) {
                    synchronized (mLock) {
                        SharedPreferences.Editor editor = context.getSharedPreferences(NetUtil.APP_LOG_ENCRYPT_COUNT, 0).edit();
                        if (NetUtil.appLogEncryptFaildCount > 2) {
                            NetUtil.appLogEncryptFaildCount += -2;
                        } else {
                            NetUtil.appLogEncryptFaildCount = 0;
                        }
                        editor.putInt("app_log_encrypt_faild_count", NetUtil.appLogEncryptFaildCount);
                        editor.apply();
                        NetUtil.d = true;
                    }
                }
            } else {
                v0 = 0;
                v2 = bytes2;
            }
            if (v2 != null && v0 != 0) {
                v0_2 = arg8 + "&tt_data=a";
                if (arg11) {
                    v0_2 = v0_2 + "&config_retry=b";
                }
                HashMap v3_3 = new HashMap();
                v3_3.put("Content-Type", "application/octet-stream;tt-data=a");
                return com.bytedance.common.utility.e.a().a(v0_2, v2, ((Map) v3_3), v1);
            }
            throw new RuntimeException("encrypt failed");
        }

        return null;
    }

    public static void a(AppContext appContext) {
        NetUtil.b = appContext;
    }

    public static String b() {
        return NetUtil.sAliYunHandler != null
                ? NetUtil.sAliYunHandler.getCloudUUID()
                : null;
    }
}

